<style scoped lang="scss">

</style>

<template>
  <div>

    <el-form :inline="true" :model="queryParams" class="demo-form-inline">
      <el-form-item label="商品名称 : ">
        <el-input v-model="queryParams.goodsName" placeholder="商品名称"></el-input>
      </el-form-item>
      <el-form-item label="品牌 : ">
        <el-input v-model="queryParams.brdCnName" placeholder="品牌"></el-input>
      </el-form-item>


      <el-form-item>
        <el-button type="primary" @click="query">查询</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" @click="handleAdd" plain icon="el-icon-plus" size="mini">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" @click="handleDelete()" plain icon="el-icon-delete" size="mini">删除</el-button>
      </el-col>
    </el-row>

    <br>

    <el-table :data="tableData" v-loading="loading" stripe border @selection-change="handleSelectionChange">

      <el-table-column
        type="selection"
        width="55">
      </el-table-column>

      <el-table-column type="expand">
        <template slot-scope="scope">
          <el-form label-position="left" inline class="demo-table-expand">
            <el-form-item label="商品主键 : ">
              <span>{{ scope.row.goodsId }}</span>
            </el-form-item>
            <br>
            <el-form-item label="一级分类 : ">
              <span>{{ scope.row.coneName }}</span>
            </el-form-item>
            <br>
            <el-form-item label="二级分类 : ">
              <span>{{ scope.row.ctwoName }}</span>
            </el-form-item>
            <br>
            <el-form-item label="三级分类 : ">
              <span>{{ scope.row.cthrName }}</span>
            </el-form-item>
            <br>
            <el-form-item label="品牌 : ">
              <span>{{ scope.row.brdCnName }}</span>
            </el-form-item>

          </el-form>
        </template>
      </el-table-column>

      <el-table-column prop="goodsName" label="商品名称">
      </el-table-column>
      <el-table-column prop="goodsCode" label="商品代号">
      </el-table-column>
      <el-table-column prop="goodsInTime" label="录入时间" :formatter="dateFormat">
      </el-table-column>
      <el-table-column prop="goodsInfo" label="详细说明" :formatter="infoFormat">
      </el-table-column>
      <el-table-column prop="modifiedTime" label="最新修改时间" :formatter="dateFormat">
      </el-table-column>

      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="primary" @click="handleEdit(scope.row.goodsId)" plain icon="el-icon-edit" size="mini">
            编辑
          </el-button>
          <el-button type="danger" @click="handleDelete(scope.row.goodsId)" plain icon="el-icon-delete"
                     size="mini">删除
          </el-button>
        </template>
      </el-table-column>

    </el-table>

    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryParams.pageNum"
      :page-sizes="[5, 10, 20, 50]"
      :page-size="10"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>

    <el-dialog :title="title" :visible.sync="dialogVisible" width="30%" append-to-body>

      <el-form ref="dialogForm" :model="dialogForm" :rules="dialogRules" lable-width="80px">

        <el-form-item label="分类" prop="goodsName">
          <el-cascader
            v-model="value"
            :options="options"
            @change="handleChangeCascader"></el-cascader>
        </el-form-item>

        <el-form-item label="一级分类" prop="coneId">
          <el-select v-model="dialogForm.coneId" clearable placeholder="请选择">
            <el-option
              v-for="item in oneList"
              :key="item.coneId"
              :label="item.coneName"
              :value="item.coneId">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="二级分类" prop="ctwoId">
          <el-select v-model="dialogForm.ctwoId" clearable placeholder="请选择">
            <el-option
              v-for="item in twoList"
              :key="item.ctwoId"
              :label="item.ctwoName"
              :value="item.ctwoId">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="三级分类" prop="cthrId">
          <el-select v-model="dialogForm.cthrId" clearable placeholder="请选择">
            <el-option
              v-for="item in threeList"
              :key="item.cthrId"
              :label="item.cthrName"
              :value="item.cthrId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="品牌" prop="brdId">
          <el-select v-model="dialogForm.brdId" clearable placeholder="请选择">
            <el-option
              v-for="item in brandList"
              :key="item.brandId"
              :label="item.brandCnName"
              :value="item.brandId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="goodsName" label="商品名称 : ">
          <el-input v-model="dialogForm.goodsName" placeholder="商品名称"></el-input>
        </el-form-item>
        <el-form-item prop="goodsCode" label="商品代号 : ">
          <el-input v-model="dialogForm.goodsCode" placeholder="商品代号"></el-input>
        </el-form-item>
        <el-form-item prop="goodsInTime" label="录入时间 : ">
          <el-date-picker type="date" v-model="dialogForm.goodsInTime" placeholder="录入时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item prop="goodsInfo" label="详细说明 : ">
          <el-input type="textarea" rows="4" v-model="dialogForm.goodsInfo" placeholder="描述"></el-input>
        </el-form-item>
        <el-form-item prop="modifiedTime" label="最新修改时间 : ">
          <el-date-picker type="date" v-model="dialogForm.modifiedTime" placeholder="最新修改时间">
          </el-date-picker>
        </el-form-item>

      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="onSubmit">确 定</el-button>
      </span>

    </el-dialog>

  </div>
</template>

<script>
import {editPage, dialogSubmit, getBasePage, deleteBase} from '@/api/webshop/GoodsSpuInfo.js'
import {getBaseList as getConeList, listCascater} from '@/api/webshop/CategoryOne'
import {getBaseList as getCtwoList} from '@/api/webshop/CategoryTwo'
import {getBaseList as getCthrList} from '@/api/webshop/CategoryThree'
import {getBrandInfoListAll} from '@/api/webshop/BrandInfo'

export default {
  name: "goodsSpuInfo",
  created() {
    this.getList()
    this.getOneList()
    this.getTwoList()
    this.getThreeList()
    this.getBrandList()
    this.getCascaterList()
  },
  watch: {
    "dialogForm.coneId"(val) {
      console.log("coneId", val)

      if (val != null) {
        this.twoList = this.twoFullList.filter(two => two.coneId == val)
      } else {
        this.twoList = this.twoFullList
      }
      console.log("val=", val)
      this.dialogForm.ctwoId = null

      if (val != null) {
        this.brandList = this.brandFullList.filter(brand => {
          return brand.coneIds.includes(val);
        })
        this.dialogForm.brdId = null
      } else {
        this.brandList = this.brandFullList
        this.dialogForm.brdId = null
      }
    },
    "dialogForm.ctwoId"(val) {
      console.log("ctwoId", val)
      if (val != null) {
        this.threeList = this.threeFullList.filter(three => three.ctwoId == val)
      } else {
        this.threeList = this.threeFullList
      }
      this.dialogForm.cthrId = null
    }
  },
  data() {
    return {

      value: [],
      options: [],


      oneList: [],
      twoList: [],
      threeList: [],
      brandList: [],
      oneFullList: [],
      twoFullList: [],
      threeFullList: [],
      brandFullList: [],


      tableData: [],
      selectedIds: [],
      title: "",
      total: 0,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        goodsId: null,
        coneId: null,
        ctwoId: null,
        cthrId: null,
        brdId: null,
        brdCnName: null,
        goodsName: null,
      },

      dialogForm: {
        coneId: null,
        ctwoId: null,
        cthrId: null,
        brdId: null,
        goodsName: null,
        goodsCode: null,
        goodsInTime: null,
        goodsInfo: null,
        modifiedTime: null
      },
      dialogRules: {
        goodsName: [
          {required: true, message: '请输入xxx', trigger: 'blur'},
          {min: 2, max: 50, message: '长度在 2 到 50 个字符', trigger: 'blur'}
        ]
      },
      loading: false,
      dialogVisible: false
    }
  },
  methods: {
    query() {
      this.pageNum = 1
      console.log("queryParams", this.queryParams)
      this.getList()
    },
    resetDialogForm() {
      this.dialogForm = {
        coneId: null,
        ctwoId: null,
        cthrId: null,
        brdId: null,
        goodsName: null,
        goodsCode: null,
        goodsInTime: null,
        goodsInfo: null,
        modifiedTime: null
      }
    },
    handleSizeChange(pageSize) {
      console.log("pageSize", pageSize)
      this.queryParams.pageSize = pageSize
      this.getList()
    },
    handleCurrentChange(pageNum) {
      console.log("pageNum", pageNum)
      this.queryParams.pageNum = pageNum
      this.getList()
    },
    dateFormat(row, column, cellValue, index) {
      if (!cellValue) {
        return ""
      }
      return cellValue ? cellValue.substring(0, 10) : cellValue
    },
    infoFormat(row, column, cellValue, index) {
      if (!cellValue) {
        return ""
      }
      return cellValue.length > 10 ? cellValue.substring(0, 10) + "..." : cellValue
    },
    getList() {
      this.loading = true
      getBasePage(this.queryParams).then(res => {
        console.log(res);
        this.tableData = res.rows
        this.total = res.total
        this.loading = false
      })
    },
    getOneList() {
      getConeList().then(res => {
        console.log(res);
        this.oneFullList = res.data
        this.oneList = this.oneFullList
        console.log(this.oneList)
      })
    },
    getTwoList() {
      getCtwoList().then(res => {

        this.twoFullList = res.data
        this.twoList = this.twoFullList
        console.log(this.twoList)
      })
    },
    getThreeList() {
      getCthrList().then(res => {
        this.threeFullList = res.data
        this.threeList = this.threeFullList
        console.log(this.threeList)
      })
    },
    getBrandList() {
      getBrandInfoListAll().then(res => {
        this.brandFullList = res.data
        this.brandList = this.brandFullList
        console.log(this.brandList)
      })
    },
    getCascaterList() {
      listCascater().then(res => {
        console.log(res);
        this.options = res.data
      })
    },
    onSubmit() {
      console.log('submit!')
      console.log(this.dialogForm)
      this.$refs["dialogForm"].validate((valid) => {
        if (valid) {

          this.loading = true

          dialogSubmit(this.dialogForm, this.title).then(res => {
            if (res.code != 200) {
              this.$message({
                message: '添加失败',
                type: 'warning'
              });
              return
            }
            this.$message({
              message: '添加成功',
              type: 'success'
            });
            this.dialogVisible = false
            this.loading = false
            this.getList()

          })
        } else {
          console.log('error submit!!');
          return false;
        }
      })

    },
    handleAdd() {
      this.resetDialogForm()
      this.title = "添加"
      this.dialogVisible = true
    },
    handleEdit(id) {
      this.loading = true
      editPage(id).then(res => {
        console.log(res);
        this.dialogForm = res.data
        this.title = "修改"
        this.loading = false
        this.dialogVisible = true
      })
    },
    handleDelete(id) {
      let ids = id || this.selectedIds.join(",")
      if (!ids) {
        this.$message({
          message: '请选择要删除的项',
          type: 'warning'
        });
        return
      }

      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteBase(ids)
          .then(res => {
            console.log(res);
            if (res.code != 200) {
              this.$message({
                message: '删除失败',
                type: 'warning'
              });
              return
            }
            this.$message({
              message: '删除成功',
              type: 'success'
            });
            this.getList()
          })
          .catch(err => {
            this.$message({
              message: '删除异常',
              type: 'error'
            });
          })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });

    },
    handleSelectionChange(val) {
      this.selectedIds = val.map(item => item.goodsId)
      console.log(this.selectedIds)
    },
    handleChangeCascader(value) {

      this.dialogForm.coneId = value[0]
      this.dialogForm.ctwoId = value[1]
      this.dialogForm.cthrId = value[2]
      console.log(value)
    }

  }
}
</script>
